const AJAX = getApp().ajax,
      $wx = getApp().$wx,
      getColor = getApp().globalData.getColor;

Page({
  data:{
  },

  onLoad(){
    this.setData({
      getColor: getColor()
    });
    
    $wx.setNavColor(getColor().val);  
   
  },

  onShow(){
    this.getUserInfo();  
  },

  onShareAppMessage() {

  },

  /*修改头像*/ 
  XIV() {
    let _this = this;
    wx.showActionSheet({
      itemList: ['打开相册','打开相机'],
      success(asRes){
        let AS_index = asRes.tapIndex, source_type = ['album','camera'];

        wx.chooseImage({
          sourceType: [source_type[AS_index]],
          count: 1,
          success(imgRes) {
            let tempFiles = imgRes.tempFiles;
            
            $wx.uploadFile({
              url: AJAX.uploadImg,
              filePath: tempFiles,
              filePathName: 'path',
              name: 'image',
              success(res){
                let file_url = JSON.parse(res.data).data.url;

                _this.updateAvatr({
                  avatar: file_url
                });

              }
            });
          }
        });
      }
   })
  },

  /**
   * 获取用户信息
   * @param token
   * */ 
  getUserInfo(){
    wx.showNavigationBarLoading();
    let _this = this, td = _this.data;

    AJAX.getUserInfo({},(status, data, msg)=>{
      wx.hideNavigationBarLoading();
      if(status){
        _this.setData({
          userInfo: data
        });
      }else{
        $wx.showToast(msg);
      }
    });
  },

  /*更新头像*/ 
  updateAvatr(params){
    let _this = this, td = _this.data;
    wx.showLoading({
      title: '更新头像中...',
      mask: true
    });

    AJAX.updateAvatr(params,(status, data, msg)=>{
      wx.hideLoading();
      if(status){
        _this.getUserInfo();
      }else{
        $wx.showToast(msg);
      }
    });
  }

});